import re

import requests
import pymysql

con = pymysql.connect(user="root", password="123456", database="wangyi")
cur = con.cursor()

url = 'https://music.163.com/discover/toplist'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
}
res = requests.get(url, headers=headers)
res = res.text
# print(res)
titles = re.findall(r'<li><a href="/song\?id=.*?">(.*?)</a></li>', res, re.S)
for title in titles:
    cur.execute("insert into top values(%s)", title)
    con.commit()
cur.close()
con.close()
